package com.baibei.module.stock.basic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.RaeTabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.baibei.basic.BasicFragmentAdapter;
import com.baibei.ebec.sdk.IQuotationApi;
import com.baibei.module.BasicFragment;
import com.baibei.module.stock.R;
import com.baibei.module.stock.simple.SimpleKLineFragment;
import com.baibei.module.stock.simple.SimpleMinuteFragment;
import com.blankj.utilcode.utils.LogUtils;

/**
 * 基础股票图表
 * Created by ChenRui on 2017/5/22 0022 10:04.
 */
public class BasicStockFragment extends BasicFragment {

    private RaeTabLayout tabLayout;
    protected ViewPager viewPager;
    private BasicFragmentAdapter adapter;
    private BasicMinuteFragment mMinuteFragment;

    public static BasicStockFragment newInstance(String productInfo) {
        Bundle args = new Bundle();
        args.putString("productInfo", productInfo);
        BasicStockFragment fragment = new BasicStockFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public BasicMinuteFragment getMinuteFragment() {
        return mMinuteFragment;
    }

    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return R.layout.fm_stock_simple;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout = (RaeTabLayout) view.findViewById(R.id.stock_layout_tab);
        viewPager = (ViewPager) view.findViewById(R.id.stock_fragment_viewpager);
        adapter = new BasicFragmentAdapter(getChildFragmentManager());
        mMinuteFragment = createMinuteFragment();
        adapter.add("价格趋势", mMinuteFragment);
        adapter.add("5分钟", createKLineFragment(IQuotationApi.TYPE_KLINE_5, 1));
        adapter.add("15分钟", createKLineFragment(IQuotationApi.TYPE_KLINE_15, 2));
        adapter.add("30分钟", createKLineFragment(IQuotationApi.TYPE_KLINE_30, 3));
        adapter.add("1小时", createKLineFragment(IQuotationApi.TYPE_KLINE_60, 4));
        adapter.add("1天", createKLineFragment(IQuotationApi.TYPE_KLINE_DAY, 5));
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setEnableIndicatorAnimate(false);

        //友盟统计
        tabLayout.addOnTabSelectedListener(new RaeTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(RaeTabLayout.Tab tab) {
                int position = tabLayout.getSelectedTabPosition();
                switch (position){
                    case 1:
                        onUmengEvent("1007");
                        break;
                    case 2:
                        onUmengEvent("1008");
                        break;
                    case 3:
                        onUmengEvent("1009");
                        break;
                    case 4:
                        onUmengEvent("1010");
                        break;
                    case 5:
                        onUmengEvent("1011");
                        break;
                }
            }

            @Override
            public void onTabUnselected(RaeTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(RaeTabLayout.Tab tab) {

            }
        });
    }

    /**
     * 分时图
     */
    protected BasicMinuteFragment createMinuteFragment() {
        return SimpleMinuteFragment.newInstance(getProductInfo());
    }

    /**
     * K线图
     */
    protected BasicNewKLineFragment createKLineFragment(String type, int position) {
        return SimpleKLineFragment.newInstance(type, position, getProductInfo());
    }

    protected String getProductInfo() {
        return getArguments().getString("productInfo");
    }
}
